/*
    The MIT License

    Copyright (c) 2010 IFMO/GameDev Studio

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
*/

#pragma once

/*-----------------------------------------------------------------------------
	Phys character :
-----------------------------------------------------------------------------*/


class EPhysCharacter : public IPhysCharacter, public btActionInterface {
	public:
							EPhysCharacter		( EPhysics *phys, float height, float radius, float climbing_slope, float stair_step );
							~EPhysCharacter		( void );
		virtual void		Move				( const EVector &dir, bool &floor, bool &ceil, bool &walls );
		virtual void		GetPose				( EPoint &position, EQuaternion &orient ) const;
		virtual void		SetPose				( const EPoint &position, const EQuaternion &orient );

		virtual void		updateAction		( btCollisionWorld* collisionWorld, btScalar deltaTimeStep );
		virtual void		debugDraw			( btIDebugDraw* debugDrawer ) {};
		
		static	void		InitGhostCallback		( btBroadphaseInterface *broadphase );
		static	void		ShutdownGhostCallback	( btBroadphaseInterface *broadphase );
		static	btOverlappingPairCallback	*overlap_pair_callback;

	protected:
		friend class EConvexCastCB;
		EPhysics	*phys;
		EQuaternion		orient;
		
		class btCharacterController2	*m_character;

		btVector3	stored_move_dir;

		float		radius;
		float		height;		
		
		struct {
			bool	floor;
			bool	ceil;
			bool	walls;
		} touch;

		btManifoldArray		manifold_array;
		btVector3	touching_normal;
		bool		touching_contact;
		
		btDynamicsWorld						*world;
		btConvexShape						*ghost_shape;
		class	btPairCachingGhostObject	*ghost_object;	
		struct MoveToObject {
			bool		reached;
			float		fraction;
			btVector3	next_move;
			btVector3	new_origin;
		};

		void		ResetTouches			( void );
		void		PreStep					( btCollisionWorld *world );
		void		MoveForwardAndStrafe	( btCollisionWorld *world, const btVector3 &move_dir );
		void		MoveToClosestObject		( btCollisionWorld *world, const btVector3 &from, const btVector3 &to, MoveToObject &move_to_obj );
		bool		RecoverFromPenetration	( btCollisionWorld *world );
		bool		RecoverFromPenetration2	( btCollisionWorld *world );
	};